﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MvcPrinciples.Logic;
using MvcPrinciples.Models;

namespace MvcPrinciples.Controllers
{
    public class RewardController : Controller
    {
        public ActionResult Index()
        {
			return View(DataProvider.GetStore.GetRewards());
        }

		public ActionResult AddReward()
		{
			return View();
		}

	    public ActionResult AddRewardToList(Reward reward)
	    {
			var imageFile = Request.Files["imageFile"];

			if (imageFile != null && imageFile.ContentLength > 0)
			{
				var imageId = Guid.NewGuid();
				imageFile.SaveAs(string.Format(Server.MapPath(string.Format("~/App_Data/{0}", imageId))));
				reward.Image = new KeyValuePair<Guid, string>(imageId, imageFile.ContentType);
			}

			DataProvider.GetStore.AddReward(reward);
			return RedirectToAction("Index", "Reward");
	    }

		public ActionResult EditReward(Guid id)
		{
			return View(DataProvider.GetStore.GetReward(id));
		}

	    public ActionResult EditRewardInList(Reward reward)
	    {
			var imageFile = Request.Files["imageFile"];

			if (imageFile != null && imageFile.ContentLength > 0)
			{
				var imageId = Guid.NewGuid();
				imageFile.SaveAs(string.Format(Server.MapPath(string.Format("~/App_Data/{0}", imageId))));
				reward.Image = new KeyValuePair<Guid, string>(imageId, imageFile.ContentType);
			}

			DataProvider.GetStore.EditReward(reward);
			return RedirectToAction("Index", "Reward");
	    }

		public ActionResult GetImage(Guid id, string type)
		{
			return File(Server.MapPath(string.Format("~/App_Data/{0}", id)), type);
		}
    }
}